package com.msconfig.mvp.view.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.bean.deviceInfoBean.TagDeviceInfo;
import com.msconfig.bean.dialogBean.EllipsoidParameterBean;
import com.msconfig.bean.dialogBean.FourParameterBean;
import com.msconfig.bean.dialogBean.ProjectionParameterBean;
import com.msconfig.bean.dialogBean.SevenParameterBean;
import com.msconfig.device.CommandParseManage;
import com.msconfig.device.CommandSendManage;
import com.msconfig.device.DeviceManage;
import com.msconfig.device.GeoLocationData;
import com.msconfig.device.GetSendCommandList;
import com.msconfig.device.TagSendCommand;
import com.msconfig.event.ClickEllipsoidParameterDeterminerEvent;
import com.msconfig.event.ClickFourParameterDeterminerEvent;
import com.msconfig.event.ClickProjectionParameterDeterminerEvent;
import com.msconfig.event.ClickSevenParameterDeterminerEvent;
import com.msconfig.event.DeviceUpdateEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpActivity;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.utils.DoubleToLocationUtil;
import com.msconfig.utils.GeneralUtil;
import com.msconfig.utils.SpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static android.view.View.VISIBLE;


public class SetupTransformationFragment extends BaseMvpFragment implements View.OnClickListener {
    private FrameLayout flLoading;
    private ImageView ivLoading;
    private Animation setupAnim;
    private ImageView ivAutoCentralMeridian;
    private GeoLocationData geoLocationData;
    private static final String CENTRALMERIDIANPOINTNUM="CENTRALMERIDIANPOINTNUM";

    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private RelativeLayout rlStartOutput;
    private ImageView ivSwitchOutputOn;
    private ImageView ivSwitchOutputOff;
    private RelativeLayout rlEllipsoidTitle;
    private RelativeLayout rlEllipsoidName;
    private TextView tvEllipsoidName;
    private RelativeLayout rlEllipsoidLongHalfshaft;
    private TextView tvEllipsoidLongHalfshaft;
    private RelativeLayout rlEllipsoidFlatteningInverse;
    private TextView tvEllipsoidFlatteningInverse;
    private RelativeLayout rlProjection;
    private RelativeLayout rlProjectionMode;
    private TextView tvProjectionMode;
    private RelativeLayout rlCentralMeridian;
    private TextView tvCentralMeridian;
    private RelativeLayout rlOther;
    private RelativeLayout rlSevenParameters;
    private TextView tvSevenParameters;
    private RelativeLayout rlFourParameters;
    private TextView tvFourParameters;
    private Button determine;
    private SevenParameterBean sevenParameterBean;
    private FourParameterBean fourParameterBean;
    private ProjectionParameterBean projectionBean;
    private EllipsoidParameterBean ellipsoidBean;
    private TagDeviceInfo deviceInfo;


    private int mStartOutput;
    private String mEllipsoidName;
    private String mEllipsoidLongHalfshaft;
    private String mEllipsoidFlatteningInverse;
    private String mProjectionMode;
    private String mCentralMeridian;
    private String mNorgaConstant;
    private String mEastgaConstant;
    private String mProjectionScale;
    private String mProjectionHeight;
    private String mBaseLatitude;
    private String mtBaseLogitude;
    private String mParallel1;
    private String mParallel2;
    private int projectionMode;

    private int mUseSevenParameter;
    private String mSevenXTranslation;
    private String mSevenYTranslation;
    private String mSevenZTranslation;
    private String mSevenXRotate;
    private String mSevenYRotate;
    private String mSevenZRotate;
    private String mSevenScale;

    private int mUseFourParameter;
    private String mFourXTranslation;
    private String mFourYTranslation;
    private String mFourRotate;
    private String mFourScale;
    private String isUse;
    private Context context;

    private MyHandler myHandler;
    private boolean isSetupDefail;
    protected static final int MSG_SET_UP_DEFAIL =1;//设置失败
    private boolean isChooseEllipsoidParameter;
    private boolean isChooseProjectionParameter;
    private boolean isChooseSevenParameter;
    private boolean isChooseFourParameter;
    private double autoCentralMeridian;

    //负责更新UI
    private static class MyHandler extends Handler {
        WeakReference<SetupTransformationFragment> weakReference;
        private MyHandler(SetupTransformationFragment fragment) {
            weakReference = new WeakReference<SetupTransformationFragment>(fragment);
        }
        @Override
        public void handleMessage(Message msg) {
            SetupTransformationFragment fragment = weakReference.get();
            if (fragment != null) {
                switch (msg.what) {
                    case MSG_SET_UP_DEFAIL:
                        if(fragment.flLoading.getVisibility()==VISIBLE){//动画还在，说明设置事件过久设置失败
                            fragment.stopAnim();
                            fragment.isSetupDefail=true;
                            fragment.showToast(R.string.toast_setup_defail);
                        }
                        break;
                }
            }
        }
    }


    public static SetupTransformationFragment newInstance() {
        Bundle args = new Bundle();
        SetupTransformationFragment fragment = new SetupTransformationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    //当Activity与fragment建立关联时调用
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_transformation;
    }

    @Override
    protected void initView(View view) {
        ivAutoCentralMeridian=view.findViewById(R.id.iv_auto_central_meridian);
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        rlStartOutput = (RelativeLayout)view.findViewById( R.id.rl_start_output );
        ivSwitchOutputOn = (ImageView)view.findViewById( R.id.iv_switch_output_on );
        ivSwitchOutputOff = (ImageView)view.findViewById( R.id.iv_switch_output_off );
        rlEllipsoidTitle = (RelativeLayout)view.findViewById( R.id.rl_ellipsoid_title );
        rlEllipsoidName = (RelativeLayout)view.findViewById( R.id.rl_ellipsoid_name );
        tvEllipsoidName = (TextView)view.findViewById( R.id.tv_ellipsoid_name );
        rlEllipsoidLongHalfshaft = (RelativeLayout)view.findViewById( R.id.rl_ellipsoid_long_halfshaft );
        tvEllipsoidLongHalfshaft = (TextView)view.findViewById( R.id.tv_ellipsoid_long_halfshaft );
        rlEllipsoidFlatteningInverse = (RelativeLayout)view.findViewById( R.id.rl_ellipsoid_flattening_inverse );
        tvEllipsoidFlatteningInverse = (TextView)view.findViewById( R.id.tv_ellipsoid_flattening_inverse );
        rlProjection = (RelativeLayout)view.findViewById( R.id.rl_projection );
        rlProjectionMode = (RelativeLayout)view.findViewById( R.id.rl_projection_mode );
        tvProjectionMode = (TextView)view.findViewById( R.id.tv_projection_mode );
        rlCentralMeridian = (RelativeLayout)view.findViewById( R.id.rl_central_meridian );
        tvCentralMeridian = (TextView)view.findViewById( R.id.tv_central_meridian );
        rlOther = (RelativeLayout)view.findViewById( R.id.rl_other );
        rlSevenParameters = (RelativeLayout)view.findViewById( R.id.rl_seven_parameters );
        tvSevenParameters = (TextView)view.findViewById( R.id.tv_seven_parameters );
        rlFourParameters = (RelativeLayout)view.findViewById( R.id.rl_four_parameters );
        tvFourParameters = (TextView)view.findViewById( R.id.tv_four_parameters );
        determine = (Button)view.findViewById( R.id.btn_determine);
        flLoading=view.findViewById(R.id.fl_loading);
        ivLoading = view.findViewById(R.id.iv_loading);

    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
        ivSwitchOutputOn.setOnClickListener( this );
        ivSwitchOutputOff.setOnClickListener( this );
        rlEllipsoidName.setOnClickListener( this );
        rlProjectionMode.setOnClickListener( this );
        rlSevenParameters.setOnClickListener( this );
        rlFourParameters.setOnClickListener( this );
        determine.setOnClickListener( this );
        ivAutoCentralMeridian.setOnClickListener( this );
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_transform);
        deviceInfo= CommandParseManage.getInstance().getDeviceInfo();
        geoLocationData = GeoLocationData.getInstance();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showEllipsoidParameter(ClickEllipsoidParameterDeterminerEvent event) {
        isChooseEllipsoidParameter =true;
        ellipsoidBean=event.getEllipsoidBean();
        tvEllipsoidName.setText(ellipsoidBean.getName());
        tvEllipsoidLongHalfshaft.setText(ellipsoidBean.getLongHalfshaft());
        tvEllipsoidFlatteningInverse.setText(ellipsoidBean.getFlatteningInverse());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showProjectionParameter(ClickProjectionParameterDeterminerEvent event) {
        isChooseProjectionParameter=true;
        projectionBean =event.getProjectionBean();
        tvProjectionMode.setText(projectionBean.getProjectionMode());
        tvCentralMeridian.setText(projectionBean.getCentralMeridian());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSevenParameter(ClickSevenParameterDeterminerEvent event) {
        isChooseSevenParameter=true;
        sevenParameterBean=event.getSevenParameterBean();
        String isUse;
        if(sevenParameterBean.getIsUse()){
            isUse="使用";
        }else{
            isUse="未使用";
        }
        tvSevenParameters.setText(isUse);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showFourParameter(ClickFourParameterDeterminerEvent event) {
        isChooseFourParameter=true;
        fourParameterBean=event.getFourParameterBean();
        if(fourParameterBean.getIsUse()){
            isUse="使用";
        }else{
            isUse="未使用";
        }
        tvFourParameters.setText(isUse);
    }

    //解析主机数据成功
    //GETALL成功
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showAnalysisGNSSDataSuccess(DeviceUpdateEvent.UpdateDialogGetRtkSuccessStatus event) {
        if (!DeviceManage.getInstance().isConnected())
            return;
        String projectionMode = "";
        if(deviceInfo.transformInfo.mStartOutput){
            onClickSwitchOutputOff();
        }else{
            onClickSwitchOutputOn();
        }
        tvEllipsoidName.setText(deviceInfo.transformInfo.mEllipsoidName);
        tvEllipsoidLongHalfshaft.setText(deviceInfo.transformInfo.mEllipsoidLongHalfshaft);
        tvEllipsoidFlatteningInverse.setText(deviceInfo.transformInfo.mEllipsoidFlatteningInverse);
        mProjectionMode=deviceInfo.transformInfo.mProjectionMode;
        switch (Integer.parseInt(mProjectionMode)){
            case 0:
                projectionMode=getString(R.string.gauss_projection);
                break;
            case 1:
                projectionMode=getString(R.string.utm_projection);
                break;
            case 2:
                projectionMode=getString(R.string.transverse_mercator_projection);
                break;
            case 3:
                projectionMode=getString(R.string.oblique_stereographic_projection);
                break;
            case 4:
                projectionMode=getString(R.string.double_stereographic_projection);
                break;
        }
        tvProjectionMode.setText(projectionMode);
        BigDecimal b = new BigDecimal((Double.parseDouble(deviceInfo.transformInfo.mCentralMeridian)*180)/Math.PI);
        double f1 = b.setScale(SpUtils.obtain().getInt(CENTRALMERIDIANPOINTNUM), BigDecimal.ROUND_HALF_UP).doubleValue();
        tvCentralMeridian.setText(String.valueOf(f1));

        if(deviceInfo.transformInfo.mIsUseSevenParameter){
            isUse="使用";
        }else{
            isUse="未使用";
        }
        tvSevenParameters.setText(isUse);
        if(deviceInfo.transformInfo.ismIsUseFourParameter){
            isUse="使用";
        }else{
            isUse="未使用";
        }
        tvFourParameters.setText(isUse);
    }

    //开始动画
    public void startAnim(){
        setupAnim = AnimationUtils.loadAnimation(context, R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        setupAnim.setInterpolator(interpolator);
        if (setupAnim != null) {
            determine.setEnabled(false);
            flLoading.setVisibility(VISIBLE);
            ivLoading.startAnimation(setupAnim);  //开始动画
        }
    }

    //停止动画
    private void stopAnim() {
        if (setupAnim != null) {
            determine.setEnabled(true);
            setupAnim=null;
            flLoading.setVisibility(View.GONE);
            ivLoading.clearAnimation();
        }
    }

    private void onClickBtnDetermine() {
        //要么选择自己选择的数据，要么就是从那边读取的数据不动
        isSetupDefail=false;
        if(!DeviceManage.getInstance().isConnected()){
            Toast.makeText(context, R.string.toast_isdisconnect, Toast.LENGTH_SHORT).show();
            return;
        }
        if(ivSwitchOutputOn.getVisibility()== View.VISIBLE){
            mStartOutput =1;
        }else{
            mStartOutput =0;
        }
        mEllipsoidName=tvEllipsoidName.getText().toString().trim();
        mEllipsoidLongHalfshaft=tvEllipsoidLongHalfshaft.getText().toString().trim();
        mEllipsoidFlatteningInverse=tvEllipsoidFlatteningInverse.getText().toString().trim();

        if(projectionBean!=null){//代表自己选择的信息
            mProjectionMode=projectionBean.getProjectionMode();
            if(mProjectionMode.equals(getString(R.string.gauss_projection))){
                projectionMode=0;
            }else if(mProjectionMode.equals(getString(R.string.utm_projection))){
                projectionMode=1;
            }else if(mProjectionMode.equals(getString(R.string.transverse_mercator_projection))){
                projectionMode=2;
            }else if(mProjectionMode.equals(getString(R.string.oblique_stereographic_projection))){
                projectionMode=3;
            }else if(mProjectionMode.equals(getString(R.string.double_stereographic_projection))){
                projectionMode=4;
            }
            mCentralMeridian=tvCentralMeridian.getText().toString().trim();
            mNorgaConstant=projectionBean.getNorgaConstant();
            mEastgaConstant=projectionBean.getEastgaConstant();
            mProjectionScale=projectionBean.getProjectionScale();
            mProjectionHeight=projectionBean.getProjectionHeight();
            mBaseLatitude=projectionBean.getBaseLatitude();
            mtBaseLogitude=projectionBean.getBaseLogitude();
            mParallel1=projectionBean.getEtParallel1();
            mParallel2=projectionBean.getEtParallel2();
        }else{//代表没有去选择数据，直接默认之前读取的数据
            projectionMode=Integer.parseInt(deviceInfo.transformInfo.mProjectionMode);
            mCentralMeridian=tvCentralMeridian.getText().toString().trim();
            mNorgaConstant=deviceInfo.transformInfo.mNorgaConstant;
            mEastgaConstant=deviceInfo.transformInfo.mEastgaConstant;
            mProjectionScale=deviceInfo.transformInfo.mProjectionScale;
            mProjectionHeight=deviceInfo.transformInfo.mProjectionHeight;
            mBaseLatitude=deviceInfo.transformInfo.mBaseLatitude;
            mtBaseLogitude=deviceInfo.transformInfo.mtBaseLogitude;
            mParallel1=deviceInfo.transformInfo.mParallel1;
            mParallel2=deviceInfo.transformInfo.mParallel2;
        }

        if(sevenParameterBean!=null){
            if(sevenParameterBean.getIsUse()){
                mUseSevenParameter=1;
            }else{
                mUseSevenParameter=0;
            }
            mSevenXTranslation=sevenParameterBean.getxTranslation();
            mSevenYTranslation=sevenParameterBean.getyTranslation();
            mSevenZTranslation=sevenParameterBean.getzTranslation();
            mSevenXRotate=sevenParameterBean.getxRotate();
            mSevenYRotate=sevenParameterBean.getyRotate();
            mSevenZRotate=sevenParameterBean.getzRotate();
            mSevenScale=sevenParameterBean.getScale();
        }else{
            if(deviceInfo.transformInfo.mIsUseSevenParameter){
                mUseSevenParameter=1;
            }else{
                mUseSevenParameter=0;
            }
            mSevenXTranslation=deviceInfo.transformInfo.mSevenXTranslation;
            mSevenYTranslation=deviceInfo.transformInfo.mSevenYTranslation;
            mSevenZTranslation=deviceInfo.transformInfo.mSevenZTranslation;
            mSevenXRotate=deviceInfo.transformInfo.mSevenXRotate;
            mSevenYRotate=deviceInfo.transformInfo.mSevenYRotate;
            mSevenZRotate=deviceInfo.transformInfo.mSevenZRotate;
            mSevenScale=deviceInfo.transformInfo.mSevenScale;
        }

        if(fourParameterBean!=null){
            if(fourParameterBean.getIsUse()){
                mUseFourParameter=1;
            }else{
                mUseFourParameter=0;
            }
            mFourXTranslation=fourParameterBean.getxTranslation();
            mFourYTranslation=fourParameterBean.getyTranslation();
            mFourRotate=fourParameterBean.getRotate();
            mFourScale=fourParameterBean.getScale();
        }else{
            if(deviceInfo.transformInfo.ismIsUseFourParameter){
                mUseFourParameter=1;
            }else{
                mUseFourParameter=0;
            }
            mFourXTranslation=deviceInfo.transformInfo.mFourXTranslation;
            mFourYTranslation=deviceInfo.transformInfo.mFourYTranslation;
            mFourRotate=deviceInfo.transformInfo.mFourRotate;
            mFourScale=deviceInfo.transformInfo.mFourScale;
        }
        //开始设置动画
        startAnim();
        //特殊，mCentralMeridian要发送弧度值，即把度转换成幅度值在发送
        //记录发送前小数点后几位，为了后面读取数据的时候和之前设置的数值一样
        SpUtils.obtain().save(CENTRALMERIDIANPOINTNUM,(mCentralMeridian.length()-mCentralMeridian.indexOf(".")-1));
        mCentralMeridian=String.valueOf((((Math.PI))/180)*(Double.parseDouble(mCentralMeridian)));

        //得到发送命令的集合
        ArrayList<TagSendCommand> sendCommandList= GetSendCommandList.getTransformationCommandList(mStartOutput, mEllipsoidName, mEllipsoidLongHalfshaft, mEllipsoidFlatteningInverse,
                projectionMode,mCentralMeridian,mProjectionScale,mNorgaConstant,mEastgaConstant,mProjectionHeight,mBaseLatitude,mtBaseLogitude,mParallel1,mParallel2,
                mUseSevenParameter,mSevenXTranslation,mSevenYTranslation,mSevenZTranslation,mSevenXRotate,mSevenYRotate,mSevenZRotate,mSevenScale,
                mUseFourParameter,mFourXTranslation,mFourYTranslation,mFourRotate,mFourScale);
        //设置发送命令的集合
        CommandSendManage.getInstance().setSendCommandList(sendCommandList);
        //开始发送命令
        CommandSendManage.getInstance().startSendCommand();

        //3s后判断有没有数据返回，没有代表设置失败
        myHandler = new MyHandler(this);
        myHandler.sendEmptyMessageDelayed(MSG_SET_UP_DEFAIL,8000);
    }

    //设置全部命令成功
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setupAllCommandSucc(DeviceUpdateEvent.DeviceCommandSendStatus event) {
        if(isSetupDefail)//3s内没有得到消息显示设置失败，防止数据延迟之后回来设置成功
            return;
        if(event.getStatus()==2){//超时，发送失败
            stopAnim();
            Toast.makeText(context,event.getFunction()+"设置失败", Toast.LENGTH_SHORT).show();
            return;
        }
        if(event.getStatus()==3){
            stopAnim();
            Toast.makeText(context,R.string.toast_setup_success, Toast.LENGTH_SHORT).show();
            backTo();

            //最后再发送一次GETALL获取最新数据
            //得到发送命令的集合
            ArrayList<TagSendCommand> sendCommandList= GetSendCommandList.getGetAllCommandList();
            //设置发送命令额集合
            CommandSendManage.getInstance().setSendCommandList(sendCommandList);
            //开始发送命令
            CommandSendManage.getInstance().startSendCommand();
        }
    }





    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }else if(v==ivSwitchOutputOn){
            onClickSwitchOutputOn();
        }else if(v==ivSwitchOutputOff){
            onClickSwitchOutputOff();
        }else if(v==rlEllipsoidName){
            onClickRlEllipsoidParameter();
        }else if(v==rlProjectionMode){
            onClickRlProjectParameter();
        }else if(v==rlSevenParameters){
            onClickRlSevenParameter();
        }else if(v==rlFourParameters){
            onClickRlFourParameter();
        }else if(v==determine){
            onClickBtnDetermine();
        }else if(v==ivAutoCentralMeridian){
            autoCentralMeridian= GeneralUtil.autoCalculationCenMeridian(tvProjectionMode.getText().toString().trim(),geoLocationData.getLongitude());
            if(geoLocationData.getLatitude() !=0 && geoLocationData.getLongitude()!=0 && autoCentralMeridian!=0){
                tvCentralMeridian.setText(String.valueOf(autoCentralMeridian));
            }else{
                Toast.makeText(context,"数据采集失败！",Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void onClickRlEllipsoidParameter() {
        EllipsoidParameterBean bean=new EllipsoidParameterBean();
        if(deviceInfo.transformInfo!=null && !isChooseEllipsoidParameter && deviceInfo.transformInfo.mEllipsoidName!=null){//第一次显示读取的数据
            bean.setName(deviceInfo.transformInfo.mEllipsoidName);
            bean.setLongHalfshaft(deviceInfo.transformInfo.mEllipsoidLongHalfshaft);
            bean.setFlatteningInverse(deviceInfo.transformInfo.mEllipsoidFlatteningInverse);
        }else{
            if(ellipsoidBean!=null){//显示自己选择的数据
                bean.setName(ellipsoidBean.getName());
                bean.setLongHalfshaft(ellipsoidBean.getLongHalfshaft());
                bean.setFlatteningInverse(ellipsoidBean.getFlatteningInverse());
            }else{
                bean=null;
            }
        }

        ((BaseMvpActivity)context).goToNextFragment(this, SetupEllipsoidParameterFragment.newInstance(bean));
    }

    private void onClickRlFourParameter() {
        FourParameterBean bean=new FourParameterBean();
        if(deviceInfo.transformInfo!=null && !isChooseFourParameter ){//第一次显示读取的数据，仅仅是为了判断有没有数据返回
           bean.setIsUse(deviceInfo.transformInfo.ismIsUseFourParameter);
           bean.setxTranslation(deviceInfo.transformInfo.mFourXTranslation);
           bean.setyTranslation(deviceInfo.transformInfo.mFourYTranslation);
           bean.setRotate(deviceInfo.transformInfo.mFourRotate);
           bean.setScale(deviceInfo.transformInfo.mFourScale);
        }else{
            if(fourParameterBean!=null){//显示自己选择的数据
                bean.setIsUse(fourParameterBean.getIsUse());
                bean.setxTranslation(fourParameterBean.getxTranslation());
                bean.setyTranslation(fourParameterBean.getyTranslation());
                bean.setRotate(fourParameterBean.getRotate());
                bean.setScale(fourParameterBean.getScale());
            }else{
                bean=null;
            }
        }
        ((BaseMvpActivity)context).goToNextFragment(this, SetupFourParameterFragment.newInstance(bean));
    }

    private void onClickRlSevenParameter() {
        SevenParameterBean bean=new SevenParameterBean();
        if(deviceInfo.transformInfo!=null && !isChooseSevenParameter ){//第一次显示读取的数据
            bean.setIsUse(deviceInfo.transformInfo.mIsUseSevenParameter);
            bean.setxTranslation(deviceInfo.transformInfo.mSevenXTranslation);
            bean.setyTranslation(deviceInfo.transformInfo.mSevenYTranslation);
            bean.setzTranslation(deviceInfo.transformInfo.mSevenZTranslation);
            bean.setxRotate(deviceInfo.transformInfo.mSevenXRotate);
            bean.setyRotate(deviceInfo.transformInfo.mSevenYRotate);
            bean.setzRotate(deviceInfo.transformInfo.mSevenZRotate);
            bean.setScale(deviceInfo.transformInfo.mSevenScale);
        }else{
            if(sevenParameterBean!=null){//显示自己选择的数据
                bean.setIsUse(sevenParameterBean.getIsUse());
                bean.setxTranslation(sevenParameterBean.getxTranslation());
                bean.setyTranslation(sevenParameterBean.getyTranslation());
                bean.setzTranslation(sevenParameterBean.getzTranslation());
                bean.setxRotate(sevenParameterBean.getxRotate());
                bean.setyRotate(sevenParameterBean.getyRotate());
                bean.setzRotate(sevenParameterBean.getzRotate());
                bean.setScale(sevenParameterBean.getScale());
            }else{
                bean=null;
            }
        }
        ((BaseMvpActivity)context).goToNextFragment(this, SetupSevenParameterFragment.newInstance(bean));
    }

    private void onClickRlProjectParameter() {
        ProjectionParameterBean bean=new ProjectionParameterBean();
        String projectionMode = "";
        if(deviceInfo.transformInfo!=null && !isChooseProjectionParameter && deviceInfo.transformInfo.mProjectionMode!=null){//第一次显示读取的数据
            switch (deviceInfo.transformInfo.mProjectionMode){
                case "0":
                    projectionMode=getString(R.string.gauss_projection);
                    break;
                case "1":
                    projectionMode=getString(R.string.utm_projection);
                    break;
                case "2":
                    projectionMode=getString(R.string.transverse_mercator_projection);
                    break;
                case "3":
                    projectionMode=getString(R.string.oblique_stereographic_projection);
                    break;
                case "4":
                    projectionMode=getString(R.string.double_stereographic_projection);
                    break;
            }
           //保留和之前设置的时候相同的小数
           bean.setProjectionMode(projectionMode);
           BigDecimal b = new BigDecimal((Double.parseDouble(deviceInfo.transformInfo.mCentralMeridian)*180)/Math.PI);
           double f1 = b.setScale(SpUtils.obtain().getInt(CENTRALMERIDIANPOINTNUM), BigDecimal.ROUND_HALF_UP).doubleValue();
           bean.setCentralMeridian(String.valueOf(f1));
           bean.setNorgaConstant(deviceInfo.transformInfo.mNorgaConstant);
           bean.setEastgaConstant(deviceInfo.transformInfo.mEastgaConstant);
           bean.setProjectionScale(deviceInfo.transformInfo.mProjectionScale);
           bean.setProjectionHeight(deviceInfo.transformInfo.mProjectionHeight);
           bean.setBaseLatitude(deviceInfo.transformInfo.mBaseLatitude);
           bean.setBaseLogitude(deviceInfo.transformInfo.mtBaseLogitude);
           bean.setEtParallel1(deviceInfo.transformInfo.mParallel1);
           bean.setEtParallel2(deviceInfo.transformInfo.mParallel2);
        }else{
            if(projectionBean!=null){//显示自己选择的数据
                bean.setProjectionMode(projectionBean.getProjectionMode());
                bean.setCentralMeridian(projectionBean.getCentralMeridian());
                bean.setNorgaConstant(projectionBean.getNorgaConstant());
                bean.setEastgaConstant(projectionBean.getEastgaConstant());
                bean.setProjectionScale(projectionBean.getProjectionScale());
                bean.setProjectionHeight(projectionBean.getProjectionHeight());
                bean.setBaseLatitude(projectionBean.getBaseLatitude());
                bean.setBaseLogitude(projectionBean.getBaseLogitude());
                bean.setEtParallel1(projectionBean.getEtParallel1());
                bean.setEtParallel2(projectionBean.getEtParallel2());
            }else{
                bean=null;
            }
        }
        ((BaseMvpActivity)context).goToNextFragment(this, SetupProjectionParameterFragment.newInstance(bean));
    }

    private void onClickSwitchOutputOff() {
        ivSwitchOutputOn.setVisibility(View.VISIBLE);
        ivSwitchOutputOff.setVisibility(View.GONE);
    }

    private void onClickSwitchOutputOn() {
        ivSwitchOutputOn.setVisibility(View.GONE);
        ivSwitchOutputOff.setVisibility(View.VISIBLE);
    }



    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

    @Override
    public void onDestroy() {
        if(myHandler!=null){
            myHandler.removeCallbacksAndMessages(null);
            myHandler=null;
        }
        stopAnim();
        super.onDestroy();
    }
}
